package cz.data.domain.masterdata.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import cz.data.common.annotation.DataInner;
import cz.data.common.base.BaseResponse;
import cz.data.domain.masterdata.model.entity.ModelColumnEntity;
import cz.data.domain.masterdata.model.entity.ModelEntity;
import cz.data.domain.masterdata.service.ModelColumnService;
import cz.data.domain.masterdata.service.ModelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
public class DataInnerController {

    @Autowired
    private ModelService modelService;
    @Autowired
    private ModelColumnService modelColumnService;

    @GetMapping("/inner/model/{id}")
    public ModelEntity getModelById(@PathVariable String id) {
        ModelEntity modelEntity = modelService.getModelById(id);
        if (Objects.nonNull(modelEntity)) {
            List<ModelColumnEntity> columns = modelColumnService.getModelColumnByModelId(modelEntity.getId());
            modelEntity.setModelColumns(columns);
        }
        return modelEntity;
    }

    @DataInner
    @PutMapping("/inner/model/flow/stage")
    public BaseResponse updateFlowStage(@RequestBody ModelEntity request) {
        LambdaUpdateWrapper<ModelEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(StringUtils.isNotBlank(request.getFlowStatus()), ModelEntity::getFlowStatus, request.getFlowStatus());
        updateWrapper.set(StringUtils.isNotBlank(request.getIsSync()), ModelEntity::getIsSync, request.getIsSync());
        updateWrapper.eq(ModelEntity::getId, request.getId());
        modelService.update(updateWrapper);
        return BaseResponse.success();
    }
}
